package com.example.demo.service;

import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 商品服务类
 */
@Service
public class ProductService {
    
    @Autowired
    private ProductRepository productRepository;
    
    public Product save(Product product) {
        return productRepository.save(product);
    }
    
    public Optional<Product> findById(Long id) {
        return productRepository.findById(id);
    }
    
    public Optional<Product> findByProductCode(String productCode) {
        return productRepository.findByProductCode(productCode);
    }
    
    public Optional<Product> findByBarcode(String barcode) {
        return productRepository.findByBarcode(barcode);
    }
    
    public List<Product> findAll() {
        return productRepository.findAll();
    }
    
    public Page<Product> findAll(Pageable pageable) {
        return productRepository.findAll(pageable);
    }
    
    public List<Product> findByCategoryId(Long categoryId) {
        return productRepository.findByCategoryId(categoryId);
    }
    
    public List<Product> findByBrand(String brand) {
        return productRepository.findByBrand(brand);
    }
    
    public List<Product> findByStatus(Product.ProductStatus status) {
        return productRepository.findByStatus(status);
    }
    
    public Page<Product> findByKeyword(String keyword, Pageable pageable) {
        return productRepository.findByKeyword(keyword, pageable);
    }
    
    public List<Product> findByCategoryIdAndStatus(Long categoryId, Product.ProductStatus status) {
        return productRepository.findByCategoryIdAndStatus(categoryId, status);
    }
    
    public boolean existsByProductCode(String productCode) {
        return productRepository.existsByProductCode(productCode);
    }
    
    public boolean existsByBarcode(String barcode) {
        return productRepository.existsByBarcode(barcode);
    }
    
    public void deleteById(Long id) {
        productRepository.deleteById(id);
    }
    
    public Product update(Product product) {
        return productRepository.save(product);
    }
}
